package com.rpframework.website.device.act.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.rpframework.core.api.FileService;
import com.rpframework.core.exception.AdminIllegalArgumentException;
import com.rpframework.core.utils.TagUtils;
import com.rpframework.utils.DateUtils;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.domain.Coupon;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.service.CouponService;
import com.rpframework.website.device.service.UserService;

/**
 * 
 *优惠券
 * */
@Controller
@RequestMapping("/admin/coupon")
public class AdminCouponAct extends AdminAct{
	@Resource CouponService service;
	@Resource FileService fileService;
	@Resource UserService userService;
	
	/**
	 * 列表
	 * */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/list")
	public String list(
			@RequestParam(value="pager", required=false) Pager pager, 
			@RequestParam(value="state", required=false) Integer state, 
			Map<Object, Object> model, RedirectAttributes attr){
		if(pager == null) {
			pager = new Pager();
		}
		if(state!=null){
			if(state==1){
				pager.getSearchMap().put("state", state+"");
				pager.getSearchMap().put("time", TagUtils.formatDate(System.currentTimeMillis()/1000));
			}
		}
		pager = service.getPager(pager);
		model.put("pager", pager);
		return this.doPackageURI("coupon/list");
	}
	/**
	 * 发布优惠券
	 * */
	@RequestMapping("/{id}/edit")
	public String edit(@PathVariable Integer id, Map<Object, Object> model,RedirectAttributes attr){
		Coupon t = service.select(id);
		if(t == null) {
			throw new AdminIllegalArgumentException("不存在的ID:" + id);
		}
		model.put("couponPager", t);
		return this.add(attr, model);
	}

	@RequestMapping("/add")
	public String add(RedirectAttributes attr, Map<Object, Object> model){
		return this.doPackageURI("coupon/edit");
	}
	@SuppressWarnings({ "rawtypes", "unchecked", "null" })
	@RequestMapping("/dosave")
	public String doSaveOrUpdate(@ModelAttribute Coupon t, 
			@RequestParam(value="coverPath", required=false) MultipartFile coverPath,
			RedirectAttributes attr){
		if(coverPath!=null && coverPath.getSize() > 0) {
			try {
				String relativelyMaplinePath = "/property/coupon/image/" + DateUtils.nowDate(DateUtils.YYYYMMDDHHMMSS) + NumberUtils.random() + "." + FilenameUtils.getExtension(coverPath.getOriginalFilename());
				fileService.saveFile(coverPath.getInputStream(), relativelyMaplinePath); 
				t.setImage(relativelyMaplinePath);
			} catch (Exception e) {
				throw new IllegalArgumentException("文件上传失败，原因:" + e.getLocalizedMessage());
			}
		} else {
			
		}
		//查询所有用户
		List<User> list = userService.searchIdList();
		List<Coupon> aList = new ArrayList<Coupon>();
		String nowTime = TagUtils.formatDate(System.currentTimeMillis()/1000);
		for(User li : list){
			Coupon c = new Coupon();
			c.setName(t.getName());
			c.setUserId(li.getId());
			c.setPrice(t.getPrice());
			c.setIntroduction(t.getIntroduction());
			c.setCreateTimes(nowTime);
			c.setStartTime(t.getStartTime());
			c.setEndTime(t.getEndTime());
			c.setImage(t.getImage());
			c.setMemo(t.getMemo());
			c.setState(1);
			aList.add(c);
		}
		if(aList!=null && aList.size()>0){
			//批量插入数据
			service.doSave(aList);
			setInfoMsg("新增操作成功！", attr);
		}else{
			setInfoMsg("发放失败！", attr);
		}
		return redirect("list");
	}
	/**
	 * 删除
	 * */
	@RequestMapping("/{id}/delete")
	public String delete(@PathVariable Integer id, Map<Object, Object> model,RedirectAttributes attr){
		Coupon t = service.select(id);
		if(t == null) {
			throw new AdminIllegalArgumentException("不存在的ID:" + id);
		}
		service.delete(id);
		setInfoMsg("删除操作成功！", attr);
		return redirect("/admin/coupon/list");
	}
	
}
